﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Notification Controller
    /// </summary>
    public class NotificationController : Controller
    {
        private const int DEFAULTPAGESIZE = 1;
        private readonly INotificationRepository _notificationRepository;

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public NotificationController() : this(new NotificationRepository())
        {
        }

        /// <summary>
        /// Notification Controller Constructor
        /// </summary>
        /// <param name="notificationRepository"></param>
        public NotificationController(INotificationRepository notificationRepository)
        {
            _notificationRepository = notificationRepository;
        }

        /// <summary>
        /// Display the listing of notification
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string strQuery, int? page)
        {
            IQueryable<Notification> notification = !string.IsNullOrWhiteSpace(strQuery)
                                          ? _notificationRepository.FindNotificationByName(strQuery).OrderBy(d => d.CreatedDate)
                                          : _notificationRepository.FindAllNotification().OrderBy(d => d.CreatedDate);
            var paginatedNotification =
                notification.ToPagedList(page.HasValue ? page.Value - 1 : 0, DEFAULTPAGESIZE);
            return View(paginatedNotification);
        }

        /// <summary>
        /// Details of Notification
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id != null)
            {
                Notification notification = _notificationRepository.GetNotificationById(id.Value);

                if (notification == null)
                {
                    return new FileNotFoundResult {Message = "No Notification found for that id"};
                }

                return View(notification);
            }

            return new FileNotFoundResult {Message = "No Notification found due to invalid notification id"};
        }

        /// <summary>
        /// Create Notification Email
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create()
        {
            var notification = new Notification();
            return View(new NotificationViewModel(notification));
        }

        /// <summary>
        /// Create Notification Email
        /// </summary>
        /// <param name="notification"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Create(Notification notification)
        {
            if (ModelState.IsValid)
            {
                notification.Title = notification.Title;
                notification.Description = HttpUtility.HtmlEncode(notification.Description);
                notification.CreatedDate = DateTime.Now.ToSqlDateTime();
                notification.IsActive = Convert.ToBoolean(Request["IsActive"]);
                _notificationRepository.Add(notification);
                _notificationRepository.Save();
                return RedirectToAction("Details", new {id = notification.NotificationId});
            }

            return View(new NotificationViewModel(notification));
        }

        /// <summary>
        /// Edit Notification
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            Notification notification = _notificationRepository.GetNotificationById(id);
            return View(new NotificationViewModel(notification));
        }

        /// <summary>
        /// Edit Notification Email
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Notification notification = _notificationRepository.GetNotificationById(id);
            if (ModelState.IsValid)
            {
                try
                {
                    notification.ModifiedDate = DateTime.Now;
                    UpdateModel(notification, "Notification");
                    _notificationRepository.Save();
                    return RedirectToAction("Details", new {id = notification.NotificationId});
                }
                catch
                {
                    return View(new NotificationViewModel(notification));
                }
            }
            return View(new NotificationViewModel(notification));
        }

        /// <summary>
        /// Delete Notification
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            Notification notification = _notificationRepository.GetNotificationById(id);
            return notification == null ? View("NotFound") : View(notification);
        }

        /// <summary>
        /// Delete Notification
        /// </summary>
        /// <param name="id"></param>
        /// <param name="confirmButton"></param>
        /// <returns></returns>
        [HttpPost, Authorize]
        public ActionResult Delete(int id, string confirmButton)
        {
            Notification notification = _notificationRepository.GetNotificationById(id);

            if (notification != null)
            {
                _notificationRepository.Delete(notification);
                _notificationRepository.Save();
                return View("Delete");
            }
            return View("NotFound");
        }

        /// <summary>
        /// Handle Unknown Action
        /// </summary>
        /// <param name="actionName"></param>
        /// <exception cref="HttpException"></exception>
        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action not found");
        }
    }
}